package com.sicnu.startup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicnu.startup.entity.Projects;
import com.sicnu.startup.exception.StartupException;
import com.sicnu.startup.mapper.ProjectsMapper;
import com.sicnu.startup.result.ResultCodeEnum;
import com.sicnu.startup.service.ProjectsService;
import com.sicnu.startup.vo.ProjectQueryVO;
import com.sicnu.startup.vo.ProjectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zr
 * @since 2022-01-08
 */
@Service
public class ProjectsServiceImpl extends ServiceImpl<ProjectsMapper, Projects> implements ProjectsService {

    //    @Cacheable(value = "hotProject", key = "'selectIndexList'")
    @Override
    public List<Projects> getProjectList() {
        QueryWrapper<Projects> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 8");
        List<Projects> projects = baseMapper.selectList(queryWrapper);
        return projects;
    }

    /**
     * 分页条件带查询
     *
     * @param projectsPage
     * @param projectQueryVO
     */
    @Override
    public void getProjectCondition(Page<Projects> projectsPage, ProjectQueryVO projectQueryVO) {
        QueryWrapper<Projects> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        // 判断条件 如果条件为空 直接查询所有
        if (projectQueryVO == null) {
            baseMapper.selectList(queryWrapper);
        } else {
            // 取出对应的条件
            // 领域
            String region = projectQueryVO.getRegion();
            String type = projectQueryVO.getType();
            String city = projectQueryVO.getCity();
            String name = projectQueryVO.getName();
            if (StringUtils.hasText(region)) {
                queryWrapper.like("region", region);
            }
            if (StringUtils.hasText(type)) {
                queryWrapper.like("type", type);
            }
            if (StringUtils.hasText(city)) {
                queryWrapper.like("city", city);
            }
            if (StringUtils.hasText(name)) {
                queryWrapper.like("name", name);
            }
            baseMapper.selectPage(projectsPage, queryWrapper);
        }

    }

    /**
     * 添加项目
     *
     * @param projectVO
     * @return
     */
    @Override
    public boolean saveProject(ProjectVO projectVO) {
        if (projectVO == null) {
            throw new StartupException(ResultCodeEnum.FAIL);
        }
        Projects projects = new Projects();
        BeanUtils.copyProperties(projectVO,projects );
        projects.setCreateTime(new Date());
        projects.setAttentionNumber(0);
        int insert = baseMapper.insert(projects);
        return insert > 0;
    }
}
